#include <ctype.h>
#include <limits.h>

int myAtoi(char *str) {
    if (str == NULL) {
        return 0;
    }
    char *ptr = str;
    int tmp = 0;
    int i = 1;
    while (1) {
        switch (*ptr) {
        case ' ':
            ptr++;
            break;
        case '-':
            i = -1;
            ptr++;
            goto _firstCharFin;
        case '+':
            i = 1;
            ptr++;
            goto _firstCharFin;
        default:
            if (isdigit(*ptr)) {
                i = 1;
                goto _firstCharFin;
            }
            else {
                goto _exit;
            }
        }
    }
    _firstCharFin:
    while (1) {
        if (isdigit(*ptr)) {
            if (i == 1) {
                if (tmp  > INT_MAX / 10 ||(tmp == INT_MAX / 10 && *ptr > 7))
                    return 0;
            }
            else {
                if (-tmp  < INT_MIN / 10 ||(tmp == INT_MIN / 10 && *ptr > 8))
                    return 0;
            }
            tmp = tmp * 10 + *ptr * i;
        }
        else {
            goto _exit;
        }
    }
    _exit:
    return tmp;
}

